from zope.interface import implements, Interface

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.interfaces import ISiteRoot
from zope.i18n.interfaces import ITranslationDomain
from zope.component import queryUtility
from zope.component import getUtility
from plone.memoize.instance import memoize
from my315ok.xtjxwcontents.interfaces.jiaxiao import Ijiaxiao
from my315ok.xtjxwcontents import xtjxwcontentsMessageFactory as _


class Ijiaxiao_searchView(Interface):
    """
    jiaxiao_search view interface
    """

    def test():
        """ test method"""


class jiaxiao_searchView(BrowserView):
    """
    jiaxiao_search browser view
    """
    implements(Ijiaxiao_searchView)

    def __init__(self, context, request):
        self.context = context
        self.request = request

    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog')

    @property
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject()
    def translates(self,item):
#        import pdb
#        pdb.set_trace()      
        
        util = queryUtility(ITranslationDomain, 'my315ok.xtjxwcontents')    
        tted = util.translate(item,target_language="zh",
                                       default="xiangtanshihexi")           
        return tted

    @memoize
    def getArea(self):

        portal = getUtility(ISiteRoot)
        catalog = getToolByName(portal, 'portal_catalog')  
        brains = catalog(object_provides=Ijiaxiao.__identifier__)
        lt = []
        for ss in brains:
            t = ss.district.strip()
            try:
                lent = len(t)
            except:
                continue

            if lent !=0 and t not in lt:
                lt.append(t)
        return lt
    
    def getImage(self,brain,scale="tile"):
        
        if scale=="orig":
            ext = "/image"
        else:
            ext = "/image_%s" % scale
        base = brain.getURL()        
#        base = self.getURL(brain)
        title = brain.Title
        url = base + '/main.png' + ext
                        
        htmltag = "<img src='%s' alt='%s' />" %(url,title)
        return htmltag

    def test(self,t,a,b):
        """ test method"""   
        if t:
            return a
        else:
            return b
